package com.cheetah.start.module.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cheetah.start.common.annotation.Log;
import com.cheetah.start.common.base.result.Result;
import com.cheetah.start.common.base.result.ResultController;
import com.cheetah.start.common.base.result.ResultTable;
import com.cheetah.start.common.enums.BusinessTypeEnum;
import com.cheetah.start.common.enums.LogTypeEnum;
import com.cheetah.start.common.query.PageQuery;
import com.cheetah.start.module.system.domain.SysDictType;
import com.cheetah.start.module.system.service.SysDictDataService;
import com.cheetah.start.module.system.service.SysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

/**
 * 字典类型控制器
 *
 * @author Feel
 * @date 2021/07/25
 */
@Api(tags = "字典类型crud")
@Slf4j
@Validated
@RestController
@RequestMapping("/system/dictType")
public class SysBaseDictTypeController extends ResultController {
    @Autowired
    private SysDictTypeService dicTypeService;
    @Autowired
    private SysDictDataService dictDataService;

    /**
     * 列表
     *
     * @param pageQuery 页面查询
     * @return {@link ResultTable}
     */
    @SaCheckPermission("sys:dictType:data")
    @ApiOperation(value = "字典类型列表")
    @GetMapping("/list")
    public ResultTable list(PageQuery pageQuery) {
        Page<SysDictType> page = new Page<>(pageQuery.getPage(), pageQuery.getLimit());
        QueryWrapper<SysDictType> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        dicTypeService.page(page, wrapper);
        return pageTable(page.getRecords(), page.getTotal());
    }

    /**
     * 添加
     *
     * @param sysDictType sys dict类型
     * @return {@link Result}
     */
    @SaCheckPermission("sys:dictType:add")
    @ApiOperation(value = "添加字典类型")
    @PostMapping("/add")
    @Log(title = "字典类型", describe = "添加", type = BusinessTypeEnum.ADD, logType = LogTypeEnum.OPERATE)
    public Result add(@Validated @RequestBody SysDictType sysDictType) {
        SysDictType one = dicTypeService.getByTypeCode(sysDictType.getTypeCode());
        if (one != null) {
            return failure("字典类型不能重复");
        }
        boolean save = dicTypeService.save(sysDictType);
        return decide(save);
    }


    /**
     * 更新
     *
     * @param sysDictType sys dict类型
     * @return {@link Result}
     */
    @SaCheckPermission("sys:dictType:edit")
    @ApiOperation(value = "编辑字典类型")
    @PostMapping("/update")
    @Log(title = "字典类型", describe = "编辑", type = BusinessTypeEnum.EDIT, logType = LogTypeEnum.OPERATE)
    public Result update(@Validated @RequestBody SysDictType sysDictType) {
        if (sysDictType.getDictTypeId() != null) {
            boolean b = dicTypeService.updateById(sysDictType);
            return decide(b, BusinessTypeEnum.EDIT);
        }
        return failure("请先添加");
    }


    /**
     * 删除
     *
     * @param dictTypeId dict类型id
     * @return {@link Result}
     */
    @SaCheckPermission("sys:dictType:delete")
    @ApiOperation(value = "删除字典类型")
    @PostMapping("/remove/{dictTypeId}")
    @Transactional(rollbackFor = Exception.class)
    @Log(title = "字典数据", describe = "删除", type = BusinessTypeEnum.REMOVE, logType = LogTypeEnum.OPERATE)
    public Result remove(@PathVariable @NotBlank(message = "不能为空") String dictTypeId) {
        SysDictType dictType = dicTypeService.getById(dictTypeId);
        String typeCode = dictType.getTypeCode();
        boolean b = dicTypeService.removeById(dictTypeId);
        // 同时删除 dictData
        dictDataService.removeByTypeCode(typeCode);
        return decide(b, BusinessTypeEnum.REMOVE);
    }


}
